<%
# OPTIONS REMOVED IN AGENT 6:
# - check_freq
# - use_mount
# - developer_mode
# - dogstreams
# - autorestart
# - custom_emitters
# - graphite
# - dogstatsd_target
# - dogstatsd_interval
# - dogstatsd_normalize
# - legacy_integrations

# TODO: options not supported yet:
# - agent_checks_interval
# - Autodiscovery (aka Service Discovery) related options
# - statsd_forward_host
# - statsd_forward_port
# - statsd_metric_namespace
# - enable_trace_agent: always enabled
# - all trace-related options are written to an INI file

def string_list_to_array(string_list)
  # return an array from a comma-separated list in a string
  string_list.split(',').map{ |elem| elem.strip }
end

## Normalize complex config values for agent6 format ##
if node['dd-agent-install']['tags'].respond_to?(:each_pair)
  tags = node['dd-agent-install']['tags'].reject{ |_k,v| v.empty? }.map{ |k,v| "#{k.strip}:#{v.strip}" }
else
  tags = string_list_to_array(node['dd-agent-install']['tags'])
end

# TODO: support the more complete proxy settings that the agent6 supports
http_proxy = nil
if node['dd-agent-install']['web_proxy']['host']
  host = node['dd-agent-install']['web_proxy']['host']
  port = node['dd-agent-install']['web_proxy']['port']
  user = node['dd-agent-install']['web_proxy']['user']
  password = node['dd-agent-install']['web_proxy']['password']
  scheme = ""

  unless host.start_with?('http://') or host.start_with?('https://')
    scheme = 'http://'
  end

  http_proxy = host
  if port
    http_proxy += ":#{port}"
  end
  if user
    if password
      http_proxy = "#{user}:#{port}@#{http_proxy}"
    else
      http_proxy = "#{user}@#{http_proxy}"
    end
  end

  http_proxy = scheme + http_proxy
end

## Populate agent_config ##
agent_config = @extra_config.merge({
  api_key: @api_key,
  dd_url: node['dd-agent-install']['url'],
  hostname: node['dd-agent-install']['hostname'],
  additional_endpoints: @additional_endpoints,
  listen_port: node['dd-agent-install']['agent_port'],
  bind_host: node['dd-agent-install']['bind_host'],
  skip_ssl_validation: node['dd-agent-install']['web_proxy']['skip_ssl_validation'],
  tags: tags,
  create_dd_check_tags: node['dd-agent-install']['create_dd_check_tags'],
  collect_ec2_tags: node['dd-agent-install']['collect_ec2_tags'],
  non_local_traffic: node['dd-agent-install']['non_local_traffic'],
  histogram_aggregates: string_list_to_array(node['dd-agent-install']['histogram_aggregates']),
  histogram_percentiles: string_list_to_array(node['dd-agent-install']['histogram_percentiles']),  # TODO: check that the value works with agent6
  use_dogstatsd: node['dd-agent-install']['dogstatsd'],
  log_level: node['dd-agent-install']['log_level'],  # TODO: make sure it's a seelog-compatible log level

  # log agent options
  logs_enabled: node['dd-agent-install']['enable_logs_agent']
})

if node['dd-agent-install']['log_file_directory']
  agent_config['log_file'] = ::File.join(node['dd-agent-install']['log_file_directory'], "agent.log")
end

if node['dd-agent-install']['use_v2_api']
  agent_config['use_v2_api'] = node['dd-agent-install']['use_v2_api']
end

if node['dd-agent-install']['syslog']['active']
  agent_config['log_to_syslog'] = true
  # TODO: double check the udp settings work with agent6
  if node['dd-agent-install']['syslog']['udp']
    agent_config['syslog_uri']  = "#{node['dd-agent-install']['syslog']['host']}:#{node['dd-agent-install']['syslog']['port']}"
  end
end

# Set proxy options
if !http_proxy.nil?
  agent_config['proxy'] = {
    http: http_proxy,
    https: http_proxy
  }
end

# Remove nil values
agent_config.reject!{ |k,v| v.nil? }

-%>
# Generated by Chef, local modifications will be overwritten

<%= JSON.parse(agent_config.to_json).to_yaml %>
